package com.eastmelon.blog.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastmelon.basic.model.Pager;
import com.eastmelon.basic.model.SystemContext;
import com.eastmelon.blog.entity.Article;
import com.eastmelon.blog.service.ArticleService;
import com.eastmelon.blog.service.ChannelService;
import com.eastmelon.blog.service.IndexService;
import com.eastmelon.blog.service.LinkService;
import com.eastmelon.blog.service.TagsService;
import com.eastmelon.blog.service.UserService;
import com.eastmelon.blog.util.FreemarkerUtil;

@Service("indexService")
public class IndexServiceImpl implements IndexService {
	@Resource(name = "articleService")
	private ArticleService articleService;
	@Resource(name = "channelService")
	private ChannelService channelService;
	@Resource(name = "linkService")
	private LinkService linkService;
	@Resource(name = "tagsService")
	private TagsService tagsService;
	@Resource(name = "userService")
	private UserService userService;

	private String outPath;
	private FreemarkerUtil util;

	@Autowired(required = true)
	public IndexServiceImpl(String ftlPath, String outPath) {
		super();
		if (util == null) {
			this.outPath = outPath;
			util = FreemarkerUtil.getInstance(ftlPath);
		}
	}

	@Override
	public void createHeader() {
		String outfile = SystemContext.getRealPath() + outPath + "/header.jsp";
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("userImage", userService.getImageName(1));
		root.put("lc", channelService.findParent());
		util.fprint(root, "/header.ftl", outfile);
	}

	@Override
	public void createBody() {
		String outfile = SystemContext.getRealPath() + outPath + "/body.jsp";
		Map<String, Object> root = new HashMap<String, Object>();
		SystemContext.setPageSize(6);
		Pager<Article> pa = articleService.findPage();
		root.put("pa", pa);
		util.fprint(root, "/body.ftl", outfile);
	}

	@Override
	public void createFooter() {
		// TODO Auto-generated method stub

	}

	@Override
	public void createNav() {
		String outfile = SystemContext.getRealPath() + outPath + "/right-nav.jsp";
		Map<String, Object> root = new HashMap<String, Object>();
		String userInfor=userService.getUserInfor(1);
		root.put("userInfor", userInfor);//个人描述
		root.put("channel", channelService.findChannelCount());
		root.put("tags", tagsService.findTagsCount());
		root.put("date",articleService.findDateCount());
		root.put("link",linkService.find());
		util.fprint(root, "/right-nav.ftl", outfile);
	}

}
